package com.example.administrator.myapplication;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    //1、定义一个变量
    Button mBtnHp;
    Button mBtnpb;

    //5.1控件变量
    ProgressBar mHpb;
    ProgressDialog pd;

    //4.2定义handler
    Handler handler = new Handler(){
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            //处理消息
            if (msg.what==101){
                int progress=msg.arg1;
                //5、更新界面
                mHpb.setProgress(progress);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //2、获取控件
        mBtnHp=(Button)findViewById(R.id.id_btn_hprogress);
        mHpb=(ProgressBar) findViewById(R.id.id_pb_h1);
        mBtnpb=(Button) findViewById(R.id.id_btn_pd);
        pd=new ProgressDialog(this);

        mBtnpb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pd.show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        int progress=0;    // 进度信息
                        while (progress<=100){
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            progress ++;
                            //4.3 发送消息
                            Message message=new Message();
                            message.what=101;
                            message.arg1=progress;
                            handler.sendMessage(message);


                        }

                        pd.dismiss();
                    }
                }).start();
            }
        });
        //3、设置事件监听
        mBtnHp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //4、编写时间代码，编写线程代码  ，模拟下载过程
                //4.1 编写线程代码
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int progress=0;    // 进度信息
                        while (progress<=100){
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            progress ++;
                      //4.3 发送消息
                            Message message=new Message();
                            message.what=101;
                            message.arg1=progress;
                            handler.sendMessage(message);


                        }
                    }
                }).start();
            }
        });
    }
}
